﻿using System.Collections.Generic;
using KLS.PPDS.ApplicationService.Manager;
using KLS.PPDS.Core.Entity;
using KLS.PPDS.Utilities.Attributes;
using KLS.PPDS.Utility;

namespace KLS.PPDS.Web.Models
{
    //View model 
    public class PublicationSaveViewModel : Publication
    {
        /// <summary>
        /// default constructor
        /// </summary>
        public PublicationSaveViewModel ()
        {
            base.InitAudit();
        }

        public int OldComapnyId { get; set; }
        public bool IsTextDisabled
        {
            get
            {
                var result = false;
                try
                {
                    if ((CommonSession.IsUserJournalist) || (CommonSession.IsUserGraphicsDesigner))
                    {
                        result = true;
                    }
                }
                catch
                {
                }
                return result;
            }
        }
    }

    public class PublicationViewModel
    {
        public int SrchCompanyId { get; set; }
        public int SrchYear { get; set; }
        public int SrchMonth { get; set; }

        public IEnumerable<Publication> Publications { get; set; } 
    }

    public class PublicationModel
    {
        public static  void ProcessIndexModel(PublicationViewModel model)
        {
            if (CommonSession.IsUserEditor || CommonSession.IsUserJournalist)
            {
                model.SrchCompanyId = CommonSession.CompanyId;
            }

            if (CommonSession.IsUserGraphicsDesigner.Equals(true))
            {
                // get pulication for assigned company
                model.Publications = PublicationManager.GetPublicationForTaskAssigned(CommonSession.UserId, model.SrchCompanyId, model.SrchYear, model.SrchMonth);
            }
            else 
            {
                model.Publications = PublicationManager.GetAllPublication(model.SrchCompanyId, model.SrchYear, model.SrchMonth);            
            }
            
        }

        public static List<Company> GetAllCompanyForModel() 
        {
            var result = CompanyManager.GetAllCompany();

            if (CommonSession.IsUserGraphicsDesigner.Equals(true))
            {
                result = CompanyManager.GetAllCompanyGraphicsUserId(CommonSession.UserId);
            }

            return result;
        }

        public static void ProcessEditModel(PublicationSaveViewModel model, int? Id)
        {
            var publication = PublicationManager.GetPublicationById(Id.GetValueOrDefault(0));
            
            if (publication!=null)
            {
                publication.CopyPropertyValue<KLS.PPDS.Core.Entity.Publication, PublicationSaveViewModel>(model);
                model.OldComapnyId = model.CompanyId;
            }
            
        }
    }
}